import { ModuleWithProviders, NgModule, Optional, SkipSelf } from '@angular/core';
import { CommonModule } from '@angular/common';
import { NbAuthModule, NbDummyAuthStrategy,
  NbDummyAuthStrategyOptions,
  NbPasswordAuthStrategy,
  NbPasswordAuthStrategyOptions,NbAuthJWTToken,NbAuthSimpleToken } from '@nebular/auth';
import { NbSecurityModule, NbRoleProvider } from '@nebular/security';
import { of as observableOf } from 'rxjs';

import { throwIfAlreadyLoaded } from './module-import-guard';
import { DataModule } from './data/data.module';

import { httpInterceptorProviders,utilsServices } from './provide.config';


export class NbSimpleRoleProvider extends NbRoleProvider {
  getRole() {
    // here you could provide any role based on any auth flow
    return observableOf('guest');
  }
}

export const NB_CORE_PROVIDERS = [
  ...DataModule.forRoot().providers,
  ...NbAuthModule.forRoot({

    strategies: [
       NbDummyAuthStrategy.setup(<NbDummyAuthStrategyOptions>{
         name: 'email',
         delay: 3000,
         token: {
           class: NbAuthSimpleToken
         }
       }),
/*      NbPasswordAuthStrategy.setup(<NbPasswordAuthStrategyOptions>{
        name: 'email',
        token: {
          class: NbAuthSimpleToken,

          key: 'token', // this parameter tells where to look for the token
          getter: function (module, res, options) { // 自己处理返回的数据的状态，{status:400, data:[{user}]}
            return res.body;
          }
        },
        baseEndpoint: 'http://192.168.0.9:8080',
        login: {
          // ...
          endpoint: '/test.do',
          method: 'get'
        },
        register: {
          // ...
          endpoint: '/api/auth/register',
        },
      }),*/
    ]
  }).providers,
  NbSecurityModule.forRoot({
    accessControl: {
      guest: {
        view: '*',
      },
      user: {
        parent: 'guest',
        create: '*',
        edit: '*',
        remove: '*',
      },
    },
  }).providers,
  {
    provide: NbRoleProvider, useClass: NbSimpleRoleProvider,
  },
  ...utilsServices,
  httpInterceptorProviders
];

@NgModule({
  imports: [
    CommonModule,
  ],
  exports: [
    //NbAuthModule,
  ],
  declarations: [],
})
export class CoreModule {
  constructor(@Optional() @SkipSelf() parentModule: CoreModule) {
    throwIfAlreadyLoaded(parentModule, 'CoreModule');
  }

  static forRoot(): ModuleWithProviders {
    return <ModuleWithProviders>{
      ngModule: CoreModule,
      providers: [
        ...NB_CORE_PROVIDERS,
      ],
    };
  }
}
